
cd "C:\Users\patri\Dropbox\My Projects\Spring 2021\Empirical IR\Data\

use "threatHR_2.dta", clear

drop if oced==1

global wc wi_v2x_polyarchy wi_lnpop wi_lngdp_pc max_civilwar2 

reg f.wi_theta_mean2 wi_v2stfisccap3 wi_threat    $wc  i.year

gen sample = 1 if e(sample)

**Model 1 (total effect of threat)

reg f.wi_theta_mean2  wi_threat  $wc i.year if sample==1, vce(hc3)
 estimates store m1
 
 egen std_wi_threat = std(wi_threat) if sample==1

 
 reg f.wi_theta_mean2  std_wi_threat  $wc i.year if sample==1, vce(hc3)
sum F.wi_theta_mean2 if sample==1
 
**threat decreases the DV (i.e. decreases respect for HR)

**Model 2 (effect of threat on capacity)

reg f.wi_v2stfisccap3 wi_threat  $wc  i.year if sample==1
 estimates store m2
 
reg f.wi_v2stfisccap3 std_wi_threat  $wc  i.year if sample==1
 sum wi_v2stfisccap3  if sample==1
 
 
**threat increase capacity 

 reg f.wi_v2stfisccap3 wi_threat  $wc  i.year , vce(hc3)
 estimates store m2_1
 *re-estimating with robust SE's

**Model 3 (effect of threat and capacity on HR)


reg f.wi_theta_mean2 wi_v2stfisccap3 wi_threat    $wc  i.year
**capacity increases respect for HR, and threat decreases it (beta for threat is the direct effect)

 estimates store m3
 suest m2 m3
 
 **Indirect Effect
nlcom (_b[m2_mean:wi_threat] * _b[m3_mean:wi_v2stfisccap3])

reg f.wi_theta_mean2 wi_v2stfisccap3 wi_threat    $wc  i.year , vce(hc3)
 estimates store m3_1

 reg f.wi_theta_mean2 wi_v2stfisccap3 std_wi_threat    $wc  i.year


reg wi_latentmean2 wi_v2stfisccap3 wi_threat    $wc  i.year
**capacity increases respect for HR, and threat decreases it (beta for threat is the direct effect)

 estimates store m4
 suest m2 m4
 
 **Indirect Effect
nlcom (_b[m2_mean:wi_threat] * _b[m4_mean:wi_v2stfisccap3])


reg f.wi_latentmean2  wi_v2stfisccap3 wi_threat    $wc i.year , vce(hc3)
 estimates store m4_1

 
 reg f.wi_PTS_S wi_v2stfisccap3  wi_threat    $wc  i.year 
**capacity increases respect for HR, and threat decreases it (beta for threat is the direct effect)

 estimates store m5
 suest m2 m5
 
 **Indirect Effect
nlcom (_b[m2_mean:wi_threat] * _b[m5_mean:wi_v2stfisccap3])

reg f.wi_PTS_S wi_v2stfisccap3 wi_threat    $wc  i.year , vce(hc3)
 estimates store m5_1

 
 poisson f.killing_estimate_mean3 wi_v2stfisccap3 wi_threat    $wc  i.year btw_killing_estimate_mean3 
estimates store m6 

**see PARAMED command below for indirect effect calculation for poisson model

*Count model robust to negbin model? Yes
nbreg f.killing_estimate_mean3 wi_v2stfisccap3 wi_threat    $wc  i.year btw_killing_estimate_mean3


 
 

lab var wi_threat "External Threat"
lab var wi_v2stfisccap3 "Capacity"
lab var wi_v2x_polyarchy "Democracy"
lab var wi_lnpop  "log of Population"
lab var wi_lngdp_pc  "log of GDP per cap"
lab var max_civilwar2 "Ongoing Civil War"


estout m1 m2_1 m3_1 m4_1 m5_1 m6, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( r2 N, fmt(%9.2f %9.0f) labels("R-Sq" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(wi_threat wi_v2stfisccap3 ) drop(*.year btw_killing_estimate_mean3) ///
mlabels ("Model 1" "Model 2" "Model 3" ) ///
title("") 

stop 
*creating dummy variables for years
tab year, gen(yr_)

*generate forward DVs
cap drop Fwi_theta_mean2
gen Fwi_theta_mean2 = F.wi_theta_mean2 if year >=1980
gen Fwi_latentmean2 = F.wi_latentmean2 if year >=1980
gen Fwi_PTS_S = F.wi_PTS_S if year >=1980
gen fkilling_estimate_mean3 = f.killing_estimate_mean3 if year>=1980

**Another way to calculate direct and indirect effect

paramed Fwi_theta_mean2 , avar(wi_threat) mvar(wi_v2stfisccap3) ///
cvars($wc  yr_* )  a0(0)  a1(1) m(0) ///
yreg(linear) mreg(linear) nointeraction

paramed Fwi_latentmean2 , avar(wi_threat) mvar(wi_v2stfisccap3) ///
cvars($wc  yr_* )  a0(0)  a1(1) m(0) ///
yreg(linear) mreg(linear) nointeraction

paramed Fwi_PTS_S, avar(wi_threat) mvar(wi_v2stfisccap3) ///
cvars($wc  yr_* )  a0(0)  a1(1) m(0) ///
yreg(linear) mreg(linear) nointeraction

paramed fkilling_estimate_mean3, avar(wi_threat) mvar(wi_v2stfisccap3) ///
cvars($wc btw_killing_estimate_mean3 )  a0(0)  a1(1) m(0) ///
yreg(poisson) mreg(linear) nointeraction

display ln(.98294606)

paramed fkilling_estimate_mean3, avar(wi_threat) mvar(wi_v2stfisccap3) ///
cvars($wc btw_killing_estimate_mean3 )  a0(0)  a1(1) m(0) ///
yreg(negbin) mreg(linear) nointeraction

display ln(.751651)
